<?php
include Kohana::find_file('controllers', 'access/access_website');
class Access_Controller extends Access_Website_Controller
{
	public function index()
	{
		$this->template->content = new View('access/access/index');
	}

	public function visitors($start_date = NULL, $end_date = NULL)
	{
		$start_date = strtotime($this->input->get('start_date', date('m/d/Y g:i A', (time()-2592000))));
		$end_date = strtotime($this->input->get('end_date', date('m/d/Y g:i A', (time()))));

		$this->template->content = new View('access/access/visitors');
		$this->template->content->logs = Auto_Modeler_ORM::factory('access_log')->fetch_where(array('date >=' => $start_date, 'date <' => $end_date));
		$this->template->content->table = Auto_Modeler_ORM::factory('access_log')->visitors($start_date, $end_date);
		$this->template->content->uniquetable = Auto_Modeler_ORM::factory('access_log')->unique_visitors($start_date, $end_date);

		// Get the general visits
		$data = Auto_Modeler_ORM::factory('access_log')->visitors($start_date, $end_date);
		$dataset = new Flot_Dataset();

		foreach ($data as $date => $visits)
		{
			$dataset->add($date*1000, count($visits));
		}

		$flot = new Flot('visitorsgraph', array('style' => 'width: 800px; height: 400px;'));
		$flot->xaxis = array('mode' => 'time', 'timeformat' => '%y/%m/%d');
		$flot->selection = array('mode' => 'x');

		$flot->add($dataset, 'Visits');

		$this->template->content->visits = $flot;

		// Find unique visitors
		$data = Auto_Modeler_ORM::factory('access_log')->unique_visitors($start_date, $end_date);
		$dataset = new Flot_Dataset();

		foreach ($data as $date => $visits)
		{
			$dataset->add($date*1000, count($visits));
		}

		$flot = new Flot('uniquevisitorsgraph', array('style' => 'width: 800px; height: 400px;'));
		$flot->xaxis = array('mode' => 'time', 'timeformat' => '%y/%m/%d');
		$flot->selection = array('mode' => 'x');

		$flot->add($dataset, 'Unique Visits');

		$this->template->content->unique_visits = $flot;
		$this->template->content->start_date = $start_date;
		$this->template->content->end_date = $end_date;
	}

	public function traffic_sources($start_date = NULL, $end_date = NULL)
	{
		$start_date = strtotime($this->input->get('start_date', date('m/d/Y g:i A', (time()-2592000))));
		$end_date = strtotime($this->input->get('end_date', date('m/d/Y g:i A', (time()))));

		$data = Auto_Modeler_ORM::factory('access_log')->fetch_where(array('date >=' => $start_date, 'date <' => $end_date));

		$this->template->content = new View('access/access/traffic_sources');
		$this->template->content->logs = Auto_Modeler_ORM::factory('access_log')->fetch_where(array('date >=' => $start_date, 'date <' => $end_date));

		$datasets = array();
		$files = array();
		$top_sources = array();
		$count = 0; // Only do 10
		foreach ($data as $visit)
		{
			$files[$visit->referrer][strtotime(date('m/d/Y', $visit->date))][] = $visit;
			$top_sources[$visit->referrer] = isset($top_sources[$visit->referrer]) ? $top_sources[$visit->referrer] + 1 : 1;
		}

		arsort($top_sources);

		foreach ($files as $referrer => $files)
		{
			if ($count++ > 10)
				break;
			// Make sure that all the dates are filled in
			for ($date = $start_date; $date <= $end_date; $date+=86400)
			{
				if ( ! isset($files[strtotime(date('m/d/Y', $date))]))
					$files[strtotime(date('m/d/Y', $date))] = array();
			}
			ksort($files);
			$dataset = new Flot_Dataset;
			foreach ($files as $date => $foobar)
			{
				//echo Kohana::debug($foobar);
				$dataset->add($date*1000, count($foobar));
			}
			$datasets[$referrer] = $dataset;
		}

		$flot = new Flot('contentgraph', array('style' => 'width: 800px; height: 400px;'));
		$flot->xaxis = array('mode' => 'time', 'timeformat' => '%y/%m/%d');
		$flot->legend = array('position' => 'sw');

		foreach ($datasets as $name => $dataset)
			$flot->add($dataset, $name);

		$this->template->content->referrers = $flot;
		$this->template->content->top_referrers = $top_sources;
		$this->template->content->start_date = $start_date;
		$this->template->content->end_date = $end_date;
	}

	public function content()
	{
		$start_date = strtotime($this->input->get('start_date', date('m/d/Y g:i A', (time()-2592000))));
		$end_date = strtotime($this->input->get('end_date', date('m/d/Y g:i A', (time()))));

		$data = Auto_Modeler_ORM::factory('access_log')->fetch_where(array('date >=' => $start_date, 'date <' => $end_date));

		$this->template->content = new View('access/access/content');
		$this->template->content->logs = $data;

		$datasets = array();
		$files = array();
		$top_content = array();
		$count = 0; // Only do 10
		foreach ($data as $visit)
		{
			$files[$visit->file][strtotime(date('m/d/Y', $visit->date))][] = $visit;
			$top_content[$visit->file] = isset($top_content[$visit->file]) ? $top_content[$visit->file] + 1 : 1;
		}

		arsort($top_content);

		foreach ($files as $file => $files)
		{
			if ($count++ > 10)
				break;
			// Make sure that all the dates are filled in
			for ($date = $start_date; $date <= $end_date; $date+=86400)
			{
				if ( ! isset($files[strtotime(date('m/d/Y', $date))]))
					$files[strtotime(date('m/d/Y', $date))] = array();
			}
			ksort($files);
			$dataset = new Flot_Dataset;
			foreach ($files as $date => $foobar)
			{
				//echo Kohana::debug($foobar);
				$dataset->add($date*1000, count($foobar));
			}
			$datasets[$file] = $dataset;
		}

		$flot = new Flot('contentgraph', array('style' => 'width: 800px; height: 400px;'));
		$flot->xaxis = array('mode' => 'time', 'timeformat' => '%y/%m/%d');
		$flot->legend = array('position' => 'sw');

		foreach ($datasets as $name => $dataset)
			$flot->add($dataset, $name);

		$this->template->content->page_content = $flot;
		$this->template->content->top_content = $top_content;
		$this->template->content->start_date = $start_date;
		$this->template->content->end_date = $end_date;
	}
}